﻿using System;
using System.Collections.Generic;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    /// <summary>
    /// 发财文章操作类
    /// </summary>
    public class DAOFCArticle : DAO
    {
        public static int Add(MFCArticle m)
        {
            String sql = "insert into fc_article (user_id,title,logo,content,state,url,date)values(@user_id,@title,@logo,@content,@state,@url,@date);select @@IDENTITY";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@title", DbType.String, m.title);
            db.AddInParameter(cmd, "@logo", DbType.String, m.logo);
            db.AddInParameter(cmd, "@content", DbType.String, m.content);
            db.AddInParameter(cmd, "@state", DbType.Int32, m.state);
            db.AddInParameter(cmd, "@url", DbType.String, m.url);
            db.AddInParameter(cmd, "@date", DbType.DateTime, m.date);
            object o = db.ExecuteScalar(cmd);
            if (o == null) return 0;
            return Convert.ToInt32(o);
        }

        public static int Delete(int id)
        {
            return ExecuteNonQuery(string.Format("delete fc_article where id={0}", id));
        }
        /// <summary>
        /// 文章设置
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static int Setting(MFCArticle m)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("update fc_article set op_total={0},op_remainder={1},op_price={2},op_starttime='{3}',op_endtime='{4}',op_plat={5}", m.op_total, m.op_remainder, m.op_price, m.op_starttime, m.op_endtime, m.op_plat);

            if (string.IsNullOrEmpty(m.op_area1))
                sb.AppendFormat(",op_area1=null");
            else
                sb.AppendFormat(",op_area1='{0}'", m.op_area1);

            if (string.IsNullOrEmpty(m.op_area2))
                sb.AppendFormat(",op_area2=null");
            else
                sb.AppendFormat(",op_area2='{0}'", m.op_area2);

            if (string.IsNullOrEmpty(m.op_area3))
                sb.AppendFormat(",op_area3=null");
            else
                sb.AppendFormat(",op_area3='{0}'", m.op_area3);

            sb.AppendFormat(" where id={0}", m.id);
            return ExecuteNonQuery(sb.ToString());
        }

        /// <summary>
        /// 设置文章的分享状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="user_id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public static int SetShared(int id, int user_id, int state)
        {
            return ExecuteNonQuery(string.Format("update fc_article set state={0} where id={1} and user_id={2}", state, id, user_id));
        }

        /// <summary>
        /// 文章分享(分享成功后，剩余数递减)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Shared(int id, int number = 1)
        {
            return ExecuteNonQuery(string.Format("update fc_article set op_remainder=op_remainder-{0} where op_remainder>0 and id={1}", number, id));
        }

        public static Dictionary<string, object> GetArea(int id)
        {
            return DAO.Get("fc_article", "id=" + id, "op_area1,op_area2,op_area3");
        }

        public static PageData GetListPage(int by_user_id, String fieldList, int pageIndex, int pageSize, String where)
        {
            PageData pageData = new PageData();
            Database database = GetDatabase();
            DbCommand cmd = database.GetStoredProcCommand("FCArticle_GetSharedList");
            database.AddInParameter(cmd, "@FieldList", DbType.String, fieldList);
            database.AddInParameter(cmd, "@Where", DbType.String, where);
            database.AddInParameter(cmd, "@PageSize", DbType.Int32, pageSize);
            database.AddInParameter(cmd, "@PageIndex", DbType.Int32, pageIndex);
            database.AddInParameter(cmd, "@by_user_id", DbType.Int32, by_user_id);

            DataSet ds = database.ExecuteDataSet(cmd);
            pageData.table = ds.Tables[0];
            pageData.pageSize = pageSize;
            pageData.pageIndex = pageIndex;

            if (pageData.table.Rows.Count > 0)
                //总页数       
                pageData.pageCount = 1;
            else
                pageData.pageCount = 0;
            pageData.itemCount = pageData.table.Rows.Count;       //数据总条数
            return pageData;
        }
    }
}